package com.imooc.distributedemo.rithm;

public class MoveZeroDemo {

    public static void moveZeroNum(int[] nums){
        int j = 0;
        for (int i = 0; i < nums.length; i++) {
            if(nums[i] != 0){
                nums[j] = nums[i];
                if(i != j){
                    nums[i] = 0;
                }
                j++;
            }
        }
    }

    public static void move(int[] nums){
        if(nums==null||nums.length==0) return;
        int count = 0;
        for (int i = 0; i < nums.length; i++) {
            if(nums[i]==0){
                count++;
            }else{
                nums[i-count] = nums[i];
            }
        }
        for (int i = nums.length-count; i < nums.length; i++) {
            nums[i] = 0;
        }
    }

    public static void moving(int[] nums){
        int snowBallSize = 0;
        for (int i = 0; i < nums.length; i++) {
            if(nums[i] == 0){
                snowBallSize++;
            }else if(snowBallSize>0){
                int t = nums[i];
                nums[i] = 0;
                nums[i-snowBallSize]=t;
            }
        }
    }

    public static void main(String[] args) {
        int[] nums = {1,0,4,2,0,8,5,0};
        // moveZeroNum(nums);
        moving(nums);
        for (int num : nums) {
            System.out.print(num+"\t");
        }
        //System.out.println(nums);
    }
}
